public class Solution2 {
    public int romanToInt(String s) {
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            char tmp = s.charAt(i);
            if (i < s.length() - 1) { //没有走到最后一个字符，可以判断
                char tmp2 = s.charAt(i + 1);
                int v = getSpecialValue(new String(new char[]{tmp, tmp2}));
                res += v;
                if (v != 0) {
                    i++;
                    continue;
                }
            }
            res += getValue(tmp);
        }
        return res;
    }

    public int getSpecialValue(String a) {
        int value = 0;
        switch (a) {
            case "IV":
                value = 4;
                break;
            case "IX":
                value = 9;
                break;
            case "XL":
                value = 40;
                break;
            case "XC":
                value = 90;
                break;
            case "CD":
                value = 400;
                break;
            case "CM":
                value = 900;
                break;
        }
        return value;
    }

    public int getValue(char str) {
        int value = 0;
        switch (str) {
            case 'I':
                value = 1;
                break;
            case 'V':
                value = 5;
                break;
            case 'X':
                value = 10;
                break;
            case 'L':
                value = 50;
                break;
            case 'C':
                value = 100;
                break;
            case 'D':
                value = 500;
                break;
            case 'M':
                value = 1000;
                break;
        }
        return value;
    }
}